
#include "../Main.h"

// My header
#include "Visuals_Main.h"

#include "../GL_Ext.h"
#include "RenderTarget.h"
#include "../Preset/Preset.h"

SDL_Window* Visuals_Main::s_pxWindow = 0;
SDL_GLContext Visuals_Main::s_xContext = 0;
Preset* Visuals_Main::s_pxPreset = 0;
RenderTarget* Visuals_Main::s_pxScratchRT = 0;
RenderTarget* Visuals_Main::s_pxRTStack[ VIS_MAIN_NUM_RT ];
int Visuals_Main::s_iCurrentTarget;
bool Visuals_Main::s_bRenderingPreset = false;

static const u_int uTEMP_Width = 640;
static const u_int uTEMP_Height = 480;

void Visuals_Main::Initialise ( Preset* pxPreset )
{
	FridgeLog("Visuals init\n");

	s_pxWindow = SDL_CreateWindow( "Fridge Visuals", 500, 100, uTEMP_Width, uTEMP_Height, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN /*| SDL_WINDOW_RESIZABLE*/ );
	s_xContext = SDL_GL_CreateContext( s_pxWindow );

	Platform_GLExt_BindFunctions();
	
	SDL_GL_SetSwapInterval( 1 );

	s_pxScratchRT = new RenderTarget( uTEMP_Width, uTEMP_Height );
	for( int iRT = 0; iRT < VIS_MAIN_NUM_RT; ++iRT )
	{
		s_pxRTStack[ iRT ] = new RenderTarget( uTEMP_Width, uTEMP_Height );
		FAssert( s_pxRTStack[ iRT ] != 0 );
	}

	s_pxPreset = pxPreset;
}


void Visuals_Main::Update ( )
{
}

void Visuals_Main::Render( )
{
	SDL_GL_MakeCurrent( s_pxWindow, s_xContext );
	
	glEnable(GL_TEXTURE_2D);
	glEnable( GL_BLEND );
	glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
	glViewport( 0, 0, uTEMP_Width, uTEMP_Height );
	glLoadIdentity ();

	// Draw to the FBO
	GetCurrentRenderTarget()->Bind();
	
	s_bRenderingPreset = true;

	s_pxPreset->Render();

	s_bRenderingPreset = false;

	GetCurrentRenderTarget()->UnBind();

	// Draw to the screen

	glBlendFunc( GL_ONE, GL_ZERO );
	
	GetCurrentRenderTarget()->FullWindowRender();
	
	glDisable(GL_TEXTURE_2D);
	
	SDL_GL_SwapWindow( s_pxWindow );
}

void Visuals_Main::Shutdown ( )
{
	delete s_pxScratchRT;

	SDL_GL_DeleteContext( s_xContext );
	SDL_DestroyWindow( s_pxWindow );
}

bool Visuals_Main::HandleEvent( const SDL_Event& xEvent )
{
	return false;
}

u_int Visuals_Main::GetWindowID()
{
	return SDL_GetWindowID( s_pxWindow );
}

u_int Visuals_Main::GetWindowHeight()
{
	return uTEMP_Height;
}

u_int Visuals_Main::GetWindowWidth()
{
	return uTEMP_Width;
}

void Visuals_Main::ResolveRT( bool bCopy )
{
	s_pxRTStack[ s_iCurrentTarget ]->UnBind();

	s_pxScratchRT->Bind();
	if( bCopy )
	{
		s_pxRTStack[ s_iCurrentTarget ]->FullWindowRender();
	}

	RenderTarget* pxSwap = s_pxRTStack[ s_iCurrentTarget ];
	s_pxRTStack[ s_iCurrentTarget ] = s_pxScratchRT;
	s_pxScratchRT = pxSwap;
}

void Visuals_Main::ResolvePreviousRT()
{
	s_pxRTStack[ s_iCurrentTarget ]->UnBind();

	s_pxScratchRT->Bind();
	s_pxRTStack[ s_iCurrentTarget + 1 ]->FullWindowRender();
	s_pxScratchRT->UnBind();

	s_pxRTStack[ s_iCurrentTarget ]->Bind();
}

void Visuals_Main::BindRTAsTexture()
{
	s_pxScratchRT->BindAsTexture();
}

void Visuals_Main::PushRenderTarget()
{
	GetCurrentRenderTarget()->UnBind();

	++s_iCurrentTarget;
	FAssert( s_iCurrentTarget < VIS_MAIN_NUM_RT );

	GetCurrentRenderTarget()->Bind();
}

void Visuals_Main::PopRenderTarget()
{
	GetCurrentRenderTarget()->UnBind();

	FAssert( s_iCurrentTarget > 0 );
	--s_iCurrentTarget;

	GetCurrentRenderTarget()->Bind();
}

//eof
